<?php
/**
 * Created by PhpStorm.
 * User: patrick wu
 * Date: 2020/10/30
 * Time: 11:01
 */
namespace App\Http\Controllers\Backend;

use App\Service\Live\OperateService;
use App\Service\Live\AnchorService;
use SwooleTW\Http\Websocket\Facades\Room;

class ConnectController extends BaseController
{
    /**
     * administrator connect event handle
     * @param $websocket
     * @param $request
     */
    public function onConnect($websocket, $request)
    {
        /**
         * bind user id
         */
        $websocket->loginUsingId((int) $this->room_id);

        $websocket->join($this->socket_room);

        /**
         *  broadcast event goods and coupons info
         */
        $operate_service    =   new OperateService();
        $goods_sort_info    =   $operate_service->goodsInfoSort($this->current_room);

        $recommend          =   $operate_service->getRecommend($this->current_room);
        $websocket->toUserId($this->room_id)->emit('backendGoods', ['goods' => $goods_sort_info, 'recommend' => $recommend]);

        if (!empty($recommend)) {
            $recommend = is_array($recommend) ? $recommend : [$recommend];
            $websocket->emit('clientRecommend', $operate_service->goodsInfo($recommend));
        }

        $coupon_sort_info =     $operate_service->couponsInfoSort($this->current_room);
        $websocket->toUserId($this->room_id)->emit('backendCoupons', ['coupons' => $coupon_sort_info]);

        //notify users
        $anchor_service  =      new AnchorService();
        $block_info      =      $anchor_service->getBlockUser($this->current_room, true);

        $websocket->toUserId($this->room_id)->emit('backendBlockUsers', $block_info);

        $online_info     =      $anchor_service->getOnlineUser($this->current_room);
        $websocket->toUserId($this->room_id)->emit('backendOnlineUsers', $online_info);

    }

    /**
     * administrator disconnect event handle
     * @param $websocket
     * @param $data
     */
    public function onDisconnect($websocket, $data)
    {

    }

}
